//入口文件
//导入vue
import Vue from 'vue';
//导入 app 的根组件
import app from './App.vue';

import 'lib-flexible'

/* //导入mint-ui
import {Header,Swipe, SwipeItem, Button,Lazyload } from 'mint-ui';
//按需导入
Vue.component(Header.name,Header); 
Vue.component(Swipe.name, Swipe);
Vue.component(SwipeItem.name, SwipeItem);
Vue.component(Button.name, Button);
Vue.use(Lazyload); */
import MintUI from 'mint-ui'
import 'mint-ui/lib/style.css'
Vue.use(MintUI)


//购物车小图标使用的样式
import './lib/mui/css/mui.min.css';
import './lib/mui/css/icons-extra.css';

// 导入路由
import VueRouter from 'vue-router';
Vue.use(VueRouter);
import router from './router.js';

//引入格式化时间的组件
import moment from 'moment'
//定义全局的过滤器
Vue.filter('dateFormat', function (dataStr, pattern = 'YYYY-MM-DD HH:mm:ss') {
    return moment(dataStr).format(pattern)
})

// 导入资源请求
import VueResource from 'vue-resource';
Vue.use(VueResource);
//设置请求的根路径
Vue.http.options.root = 'http://www.liulongbin.top:3005/api/';
Vue.http.options.emulateJSON = true;

//安装图片预览插件
import VuePreview from 'vue-preview'
// defalut install
Vue.use(VuePreview)

//注册 vuex 
import Vuex from 'vuex'
Vue.use(Vuex)

var car = JSON.parse(localStorage.getItem('car') || '[]')
var store = new Vuex.Store({
    state: { //this.$store.state.***
        car: car
    },
    mutations: { //this.$store.commit('方法名称','按需传入唯一的参数')
        addToCar(state, goodsinfo) {
            var flag = false
            state.car.some(item => {
                if (item.id === goodsinfo.id) {
                    item.count += parseInt(goodsinfo.count)
                    flag = true
                    return true
                }
            })
            if (!flag) {
                state.car.push(goodsinfo)
            }
            localStorage.setItem('car', JSON.stringify(state.car))
        },
        updateGoodsInfo(state, goodinfo) {
            state.car.some(item => {
                //console.log(item)
                //console.log(goodinfo)
                //数据类型不一样，不能用全等
                if (item.id == goodinfo.id) {
                    item.count = parseInt(goodinfo.count)
                    return true;
                } else {
                    console.log('fail...')
                }
            })
            localStorage.setItem('car', JSON.stringify(state.car))
        },
        removeFormCar(state, id) {
            state.car.some((item, index) => {
                if (item.id == id) {
                    state.car.splice(index, 1)
                    return true;
                }
            })
            localStorage.setItem('car', JSON.stringify(state.car))
        },
        updatedSelected(state, info){
            state.car.some(item => {
                if(item.id == info.id){
                    item.selected = info.selected
                    return true;
                }
            })
            localStorage.setItem('car', JSON.stringify(state.car))
        }
    },
    getters: { //this.$store.getters.***
        getAllCount(state) {
            var c = 0;
            state.car.forEach(item => {
                c += item.count
            })
            return c
        },
        getGoodsCount(state) {
            var o = {}
            state.car.forEach(item => {
                o[item.id] = item.count;
            })
            return o;
        },
        getGoodsSelected(state) {
            var o = {}
            state.car.forEach(item => {
                o[item.id] = item.selected
            })
            return o
        },
        getGoodsCountAndAmount(state){
            var o = {
                count: 0,
                amount: 0
            }
            state.car.forEach(item => {
                if(item.selected){
                    o.count += item.count
                o.amount += item.price * item.count
                }
            })
            return o;

        }
    }
})


var vm = new Vue({
    el: '#app',
    data: {},
    render: a => a(app),
    router: router,
    store: store
})